ActionHandler = {}


function ActionHandler.Medic_Drag()
	if EntityToCheck ~= 0 then
		SceneEntityMedical2 = EntityToCheck
		local DeadPedPosition = natives.ENTITY.GET_ENTITY_COORDS(SceneEntityMedical2, true)
		natives.STREAMING.REQUEST_ANIM_DICT("combat@drag_ped@entry")
		natives.STREAMING.REQUEST_ANIM_DICT("combat@drag_ped@")
		natives.AI.CLEAR_PED_TASKS_IMMEDIATELY(plyPed)
		scene3 = natives.PED.CREATE_SYNCHRONIZED_SCENE(DeadPedPosition.x, DeadPedPosition.y, DeadPedPosition.z+1, 0.0, 0.0, 180.0, 2)
		natives.PED.SET_SYNCHRONIZED_SCENE_LOOPED(scene3, false)
		natives.AI.TASK_SYNCHRONIZED_SCENE(SceneEntityMedical2, scene3, "combat@drag_ped@", "injured_pickup_back_ped", 1000.0, -4.0, 64, 0, 0x447a0000, 0)
		natives.AI.TASK_SYNCHRONIZED_SCENE(plyPed, scene3, "combat@drag_ped@entry", "injured_pickup_90_plyr", 1000.0, -4.0, 1, 0, 0x447a0000, 0)
		natives.PED.SET_SYNCHRONIZED_SCENE_PHASE(scene3, 0.0)
	
	
	end
end
function ActionHandler.Medic_Inspect()
		if EntityToCheck ~= 0 then
			SceneEntityMedical = EntityToCheck
			streaming.RequestAnimDict("amb@medic@standing@tendtodead@enter")
			natives.AI.CLEAR_PED_TASKS_IMMEDIATELY(plyPed)
			natives.AI.TASK_TURN_PED_TO_FACE_ENTITY(plyPed, EntityToCheck, -1)
			if natives.PED.IS_PED_FACING_PED(plyPed, EntityToCheck, 20) == true then
				local DeadPedPosition = natives.ENTITY.GET_ENTITY_COORDS(plyPed, true)
				local DeadPedHeading = natives.ENTITY.GET_ENTITY_HEADING(plyPed)
				scene1 = natives.PED.CREATE_SYNCHRONIZED_SCENE(DeadPedPosition.x, DeadPedPosition.y,DeadPedPosition.z, 0.0, 0.0, DeadPedHeading-15, 1)
				natives.PED.SET_SYNCHRONIZED_SCENE_LOOPED(scene1, false)
				natives.AI.TASK_SYNCHRONIZED_SCENE(plyPed, scene1, "amb@medic@standing@tendtodead@enter", "enter", 1000.0, -4.0, 1, 0, 0x447a0000, 0)
				natives.PED.SET_SYNCHRONIZED_SCENE_PHASE(scene1, 0.0)
				MedicHelper = true
			end
		end
end

-- ActionHandler tick
function ActionHandler.tick()
	
	if CheckSystemActivator then
		if DoingAnything == false then
			if IsKeyDown(KEY_Q) == true and EntityToCheck == 0 then
				natives.AI.TASK_START_SCENARIO_IN_PLACE(plyPed, "WORLD_HUMAN_SMOKING", -1, true)
			end
			if natives.AI.IS_PED_ACTIVE_IN_SCENARIO(plyPed) == true then
				if IsKeyDown(KEY_E) == true and EntityToCheck == 0 then
					natives.STREAMING.REQUEST_ANIM_DICT("amb@world_human_smoking@male@male_a@exit")
					local PlayerPedPosition = natives.ENTITY.GET_ENTITY_COORDS(plyPed, true)
					local PlayerPedHeading = natives.ENTITY.GET_ENTITY_HEADING(plyPed)
					scene1 = natives.PED.CREATE_SYNCHRONIZED_SCENE(PlayerPedPosition.x, PlayerPedPosition.y, PlayerPedPosition.z, 0.0, 0.0, PlayerPedHeading, 1)
					natives.PED.SET_SYNCHRONIZED_SCENE_LOOPED(scene1, false)
					natives.AI.TASK_SYNCHRONIZED_SCENE(plyPed, scene1, "amb@world_human_smoking@male@male_a@exit", "exit", 1000.0, -4.0, 1, 0, 0x447a0000, 0)
					natives.PED.SET_SYNCHRONIZED_SCENE_PHASE(scene1, 0.0)
					ThrowingAwayCig = true
				end
			end
			if IsKeyDown(KEY_E) == true and EntityToCheck == 0 then
				
			end
		end
	end
	
	if CheckSystemActivator then
		if DoingAnything == false then
			if IsKeyDown(KEY_Q) == true and EntityToCheck == 0 then

			end
			if IsKeyDown(KEY_E) == true and EntityToCheck == 0 then

			end
		end
	end

	if ThrowingAwayCig then
		local AnimTime = natives.PED.GET_SYNCHRONIZED_SCENE_PHASE(scene1)
		if AnimTime == 1 then
			natives.AI.CLEAR_PED_TASKS_IMMEDIATELY(plyPed)
			ThrowingAwayCig = false
		end
	end
	
	
	if Medic_Drag then 
		DoingAnything = true
		if IsKeyDown(KEY_Q) then 
			ActionHandler.Medic_Drag()
		end
	end
	if Medic_Inspect then
		DoingAnything = true
		if IsKeyDown(KEY_E) then 
			ActionHandler.Medic_Inspect()
		end
	end
	
	if MedicHelper then
		local AnimTime = natives.PED.GET_SYNCHRONIZED_SCENE_PHASE(scene1)
		if AnimTime == 1 then 
			print("YES!")
			streaming.RequestAnimDict("amb@medic@standing@tendtodead@base")
			local DeadPedPosition = natives.ENTITY.GET_ENTITY_COORDS(plyPed, true)
			local DeadPedHeading = natives.ENTITY.GET_ENTITY_HEADING(plyPed)
			scene1 = natives.PED.CREATE_SYNCHRONIZED_SCENE(DeadPedPosition.x,DeadPedPosition.y,DeadPedPosition.z, 0.0, 0.0, DeadPedHeading, 2)
			natives.PED.SET_SYNCHRONIZED_SCENE_LOOPED(scene1, false)
			natives.AI.TASK_SYNCHRONIZED_SCENE(plyPed, scene1, "amb@medic@standing@tendtodead@base", "base", 1000.0, -4.0, 1, 0, 0x447a0000, 0)
			natives.PED.SET_SYNCHRONIZED_SCENE_PHASE(scene1, 0.0)
		end
		if IsKeyDown(KEY_R) == true then
			HealHelper = true
			streaming.RequestAnimDict("amb@medic@standing@tendtodead@idle_a")
			local DeadPedPosition = natives.ENTITY.GET_ENTITY_COORDS(plyPed, true)
			local DeadPedHeading = natives.ENTITY.GET_ENTITY_HEADING(plyPed)
			scene2 = natives.PED.CREATE_SYNCHRONIZED_SCENE(DeadPedPosition.x,DeadPedPosition.y,DeadPedPosition.z, 0.0, 0.0, DeadPedHeading, 2)
			natives.PED.SET_SYNCHRONIZED_SCENE_LOOPED(scene2, false)
			natives.AI.TASK_SYNCHRONIZED_SCENE(plyPed, scene2, "amb@medic@standing@tendtodead@idle_a", "idle_a", 1000.0, -4.0, 1, 0, 0x447a0000, 0)
			natives.PED.SET_SYNCHRONIZED_SCENE_PHASE(scene2, 0.0)
		end
		if IsKeyDown(KEY_Q) == true then
				print("YES2!")
				streaming.RequestAnimDict("amb@medic@standing@tendtodead@exit")
				local DeadPedPosition = natives.ENTITY.GET_ENTITY_COORDS(plyPed, true)
				local DeadPedHeading = natives.ENTITY.GET_ENTITY_HEADING(plyPed)
				scene1 = natives.PED.CREATE_SYNCHRONIZED_SCENE(DeadPedPosition.x,DeadPedPosition.y,DeadPedPosition.z, 0.0, 0.0, DeadPedHeading, 2)
				natives.PED.SET_SYNCHRONIZED_SCENE_LOOPED(scene1, false)
				natives.AI.TASK_SYNCHRONIZED_SCENE(plyPed, scene1, "amb@medic@standing@tendtodead@exit", "exit", 1000.0, -4.0, 1, 0, 0x447a0000, 0)
				natives.PED.SET_SYNCHRONIZED_SCENE_PHASE(scene1, 0.0)
				PressedZ = true
		end
		if AnimTime == 1 and PressedZ == true then
			MedicHelper = false
			natives.AI.CLEAR_PED_TASKS_IMMEDIATELY(plyPed)
			PressedZ = false
			Medic_Inspect = false
			HealHelper = false
			DoingAnything = false
		end
	end			

	if HealHelper then
		local AnimTime2 = natives.PED.GET_SYNCHRONIZED_SCENE_PHASE(scene2)
		if AnimTime2 == 1 then
			print("hi")
			natives.PED.RESURRECT_PED(SceneEntityMedical)
			natives.ENTITY.SET_ENTITY_VELOCITY(SceneEntityMedical, 0, 0, 0)
			natives.PED.SET_PED_TO_RAGDOLL(SceneEntityMedical, 1000, 1000, 0, false, false, false)
			natives.AI.CLEAR_PED_TASKS_IMMEDIATELY(SceneEntityMedical)
			HealHelper = false
		end
	end

end


-- ActionHandler init
function ActionHandler.init()
	MedicHelper = false
	Medic_Inspect = false
	Medic_Drag = false
	PressedZ = false
	HealHelper = false
	ThrowingAwayCig = false
	DoingAnything = false

end

-- ActionHandler Onerror 
function ActionHandler:OnError()
	--self:Kill()
	--addon.Load("RENAMEME")
end